package com.vivo.java.javaref.graphics;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public class SnippetsBufferedImage extends Frame {

	/**
         * 
         */
        private static final long serialVersionUID = 1L;

	/**
	 * e666. 创建缓冲图像
	 * 
	 * A buffered image is a type of image whose pixels can be modified. For example, you can draw on a buffered
	 * image and then draw the resulting buffered image on the screen or save it to a file. A buffered image
	 * supports many formats for storing pixels. Although a buffered image of any format can be drawn on the screen,
	 * it is best to choose a format that is the most compatible with the screen to allow efficient drawing. This
	 * example demonstrates several ways of creating a buffered image. If the buffered image will not be drawn, it
	 * can simply be constructed with a specific format; TYPE_INT_RGB and TYPE_INT_ARGB are typically used. See
	 * BufferedImage for a list of available formats.
	 */
	public void e666创建缓冲图像() {

		int width = 100;
		int height = 100;

		// Create buffered image that does not support transparency
		BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

		// Create a buffered image that supports transparency
		bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

		/**
		 * These examples create buffered images that are compatible with the screen:
		 */
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice gs = ge.getDefaultScreenDevice();
		GraphicsConfiguration gc = gs.getDefaultConfiguration();

		// Create an image that does not support transparency
		bimage = gc.createCompatibleImage(width, height, Transparency.OPAQUE);

		// Create an image that supports transparent pixels
		bimage = gc.createCompatibleImage(width, height, Transparency.BITMASK);

		// Create an image that supports arbitrary levels of transparency
		bimage = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);

		/**
		 * A screen compatible buffered image can also be created from a graphics context:
		 */

		// public void paint(Graphics g) {
		// prepare
		Graphics g = null;// come from public void paint
		// prepare~

		Graphics2D g2d = (Graphics2D) g;

		// Create an image that does not support transparency
		bimage = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.OPAQUE);

		// Create an image that supports transparent pixels
		bimage = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.BITMASK);

		// Create an image that supports arbitrary levels of transparency
		bimage = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
		// }

		/**
		 * One last way of 创建缓冲图像 is using Component.createImage(). This method can be used only if the
		 * component is visible on the screen. Also, this method returns buffered images that do not support
		 * transparent pixels.
		 */
		// prepare
		Component component = new JPanel();
		// prepare~
		bimage = (BufferedImage) component.createImage(width, height);
		if (bimage == null) {
			// The component is not visible on the screen
		}

	}

	public void e672缩放_剪取_移动_翻转缓冲图像() {
		// prepare
		BufferedImage bimage = null;
		int scalex = 0, scaley = 0, shiftx = 0, shifty = 0, x = 0, y = 0, radians = 0;
		// prepare~

		AffineTransform tx = new AffineTransform();
		tx.scale(scalex, scaley);
		tx.shear(shiftx, shifty);
		tx.translate(x, y);
		tx.rotate(radians, bimage.getWidth() / 2, bimage.getHeight() / 2);

		AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
		bimage = op.filter(bimage, null);

	}

	public void 变换Graphics() {
		// prepare
		BufferedImage bimage = null;
		// prepare~

		Graphics2D g2 = (Graphics2D) bimage.getGraphics();
		g2.setPaint(Color.blue);
		g2.fill3DRect(20, 20, 20, 20, false);
		g2.translate(getWidth() / 2, getHeight() / 2);
		AffineTransform t = new AffineTransform();
		t.setToRotation(Math.toRadians(30));
		g2.transform(t);

	}
}
